﻿/**
//  CameraController.cs
//  Marquee Moment
//  Copyright 2011 Systematix Technocrates. All rights reserved.
**/
using System;
using MarqueeMoment.CameraController.Command;

namespace MarqueeMoment.CameraController.Camera
{
    public class CameraController
{

	//// Camera model

	protected CameraModel model;
	//// Command processing

	protected Processor processor = new Processor();

	//// Constractor
	public CameraController()
	{
		model = null;
	}

	//// Destructor
	//protected override void Finalize()
    ~CameraController()
	{
	}


	public void setCameraModel(CameraModel model)
	{
		this.model = model;
	}


	//// Start processor thread   

	public void run()
	{
		processor.start();

		//The communication with the camera begins
		StoreAsync(new OpenSessionCommand(model));

	}


	public void actionPerformed(string strEvent, IntPtr inObject)
	{
		if (strEvent == "download") {
			StoreAsync(new DownloadCommand(model, inObject));
			// 
		}
	}



	public void actionPerformed(string strEvent)
	{

		if (strEvent == "opensession") {
			//// Start communication with remote camera.
			StoreAsync(new OpenSessionCommand(model));

		} else if (strEvent == "takepicture") {
			StoreAsync(new TakePictureCommand(model));

		} else if (strEvent == "close") {
            model.notifyObservers(Command.commandName.clse,0);
			processor.setCloseCommand(new CloseSessionCommand(model));
			processor.stopTh();
			processor.@join();
		}

	}


	public void actionPerformed(string strEvent, uint id, uint data)
	{
		if (strEvent == "get") {
			StoreAsync(new GetPropertyCommand(model, id));

		} else if (strEvent == "set") {
			StoreAsync(new SetPropertyCommand(model, id, data));

		} else if (strEvent == "getlist") {
			StoreAsync(new GetPropertyDescCommand(model, id));

		}

	}


	//// Receive a command
	protected void StoreAsync(Command.Command command)
	{
		if ((command == null) == false) {
			processor.enqueue(command);

		}

	}


}
}
